package com.example.boot07test01.controller;


import com.example.boot07test01.entity.Emp;
import com.example.boot07test01.mapper.EmpMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class EmpController {
    @Autowired
    EmpMapper mapper;

    @RequestMapping("insertEmp1")
    public void insertEmp(@RequestBody Emp emp){
        System.out.println("emp = " + emp);
        mapper.insertEmp(emp);
    }
    @RequestMapping("selectEmp1")
    public String selectEmp(){
        List<Emp> list = mapper.selectEmp();
        //把list集合中的数据装金表格标签
        String html = "<table border='1'>";
        html+="<caption>员工列表</caption>";
        html+="<tr><th>id</th><th>姓名</th><th>工资</th><th>工作</th><th>操作</th></tr>";
        for (Emp emp :
                list) {
            html+="<tr>";
            html+="<td>"+emp.getId()+"</td>";
            html+="<td>"+emp.getName()+"</td>";
            html+="<td>"+emp.getSalary()+"</td>";
            html+="<td>"+emp.getJob()+"</td>";
            //http://localhost:8080/delete?id=xxx
            html+="<td><a href='/deleteEmp1?id="+emp.getId()+"'>删除</a></td>";
            html+="</tr>";
        }
        html+="</table>";
        return html+"<br><a href='/'>返回首页</a>";

    }
    @RequestMapping("deleteEmp1")
    public String deleteEmpById(int id){
        mapper.deleteEmpById(id);
        return "<br><a href='/'>返回首页</a>";
    }
    @RequestMapping("updateEmp1")
    public void updateEmp(@RequestBody Emp emp){
        mapper.updateEmp(emp);
    }
}
